package com.tl.commonlibrary.ui.picture.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.AppCompatImageView;
import android.view.View;
import android.view.ViewGroup;

import com.luck.picture.lib.entity.LocalMedia;
import com.squareup.picasso.Picasso;
import com.tl.commonlibrary.R;
import com.tl.commonlibrary.tool.UIUtils;
import com.tl.commonlibrary.ui.adapter.IAdapter;
import com.tl.commonlibrary.ui.adapter.ViewHolder;
import com.tl.commonlibrary.ui.picture.bean.Picture;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/6/6 0006.
 */

public abstract class PictureUploadAdapter extends IAdapter<Picture> {
    public static final int TYPE_PICTURE = 0;
    public static final int TYPE_PICTURE_ADD = 1;
    public static final int FROM_POUND_UPLOAD = 1;//磅单上传
    public static final int FROM_ATTACHMENT_UPLOAD = 2;//附件上传
    public static final int FROM_PRODUCT_SOURCE_UPLOAD = 3;//图片上传
    public static final int FROM_SUPPLY_UPLOAD = 6;//供应图片上传
    public static final int FROM_DEMAND_UPLOAD = 7;//求购图片上传
    public int maxSize = 9;
    /**
     * 当前文件列表所附属的业务id，如订单号，商品号等
     */
    protected long businessId;
    protected List<Picture> pounds;

    public PictureUploadAdapter(Context context, List<Picture> datas) {
        super(context, datas, new int[]{R.layout.item_picture_upload, R.layout.item_picture_upload_add});
        this.pounds = datas;
    }

    public void setMaxSize(int maxSize) {
        this.maxSize = maxSize;
    }

    public int getMaxSize() {
        return maxSize;
    }

    @Override
    protected void getView(ViewHolder viewHolder, Picture entity, int position, View convertView, boolean isConvert, int itemViewType, ViewGroup parent) {
        switch (itemViewType) {
            case TYPE_PICTURE:
                AppCompatImageView deleteIBtn = viewHolder.getView(R.id.deleteIBtn);
                AppCompatImageView imageIView = viewHolder.getView(R.id.imageIBtn);
                Uri uri = entity.getUri();
                if (uri != null) {
                    Picasso.with(convertView.getContext()).load(uri).placeholder(R.drawable.ic_default).error(R.drawable.ic_default).into(imageIView);
                } else {
                    imageIView.setImageResource(R.drawable.ic_default);
                }
                convertView.setOnClickListener(this);
                deleteIBtn.setOnClickListener(this);
                deleteIBtn.setTag(R.id.tag_id, entity);
                convertView.setTag(R.id.tag_id, entity);
                break;
            case TYPE_PICTURE_ADD://添加图片
                View addPictureBtn = viewHolder.getView(R.id.addPictureBtn);
                addPictureBtn.setTag(R.id.tag_id, TYPE_PICTURE_ADD);
                addPictureBtn.setOnClickListener(this);
                break;
            default:break;
        }

    }

    @Override
    public void addData(Picture data) {
        if (getDataSize() >= maxSize) {
            return;
        }
        super.addData(data);
    }

    @Override
    public void addData(List<Picture> datas) {
        if (getDataSize() >= maxSize) {
            return;
        }
        super.addData(datas);
    }

    /**
     * 拿到列表中的本地路径列表
     *
     * @return
     */
    public ArrayList<LocalMedia> filterLocalMedias() {
        ArrayList<LocalMedia> medias = null;
        int len = getDataSize();
        if (len > 0) {
            medias = new ArrayList<>();
            for (int i = 0; i < len; i++) {
                medias.add(getItem(i).getLocalMedia());
            }
        }
        return medias;
    }

    protected boolean isFileOperating = false;

    /**
     * 是否有文件正在上传或者正在删除，只能同时操作一种
     *
     * @return
     */
    public boolean checkIsFileOperating() {
        if (isFileOperating) {
            UIUtils.showToast(R.string.progress_file_operating);
            return true;
        }
        return false;
    }


    /**
     * 还能导入多少张图片
     *
     * @return
     */
    public int getLimitSize() {
        return maxSize - getDataSize();
    }

    public boolean isLimited() {
        return getLimitSize() <= 0;
    }

    @Override
    public int getCount() {
        return mDatas != null ? mDatas.size() + 1 : 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position < mDatas.size()) {
            return TYPE_PICTURE;
        }
        return TYPE_PICTURE_ADD;
    }

    @Override
    public Picture getItem(int position) {
        if (position < mDatas.size()) {
            return mDatas.get(position);
        }
        return null;
    }

    public long getBusinessId() {
        return businessId;
    }

    public void setBusinessId(long businessId) {
        this.businessId = businessId;
    }

}
